package org.mclll.xwangplugin.resourcePack;

import cn.hutool.http.HttpUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.HashMap;

/**
 * @author llB
 * @date 2024-06-21 12:28
 * @description todo
 */
public class NetWork {

    public static String BASE_URL = "http://127.0.0.1:8889/pack";

    private static String PACK_NAME = null;

    public static String getPackName() {
        return HttpUtil.get(BASE_URL + "/getHash");
    }

    public static void setPlayerPack(Player player) {
        if (NetWork.PACK_NAME == null) {
            NetWork.PACK_NAME = NetWork.getPackName();
        }
        player.setResourcePack(NetWork.BASE_URL + "/download?fileName=" + PACK_NAME);
    }

    public static void upPack(File dataFolder, CommandSender sender) {
        if (sender == null) {
            sender = Bukkit.getConsoleSender();
        }
        HashMap<String, Object> paramMap = new HashMap<>();
        //文件上传只需将参数中的键指定（默认file），值设为文件对象即可，对于使用者来说，文件上传与普通表单提交并无区别
        File pack = new File(dataFolder.getParentFile(), "ItemsAdder/output/generated.zip");
        paramMap.put("file", pack);
        sender.sendMessage(Component.text("材质上传中。。。。。。。。").color(NamedTextColor.GREEN));
        String result = HttpUtil.post(NetWork.BASE_URL + "/upload", paramMap);
        PACK_NAME = result;
        sender.sendMessage(!"false".equals(result) ? Component.text("上传中成功: " + result).color(NamedTextColor.GREEN) : Component.text("上传中失败！！！").color(NamedTextColor.RED));
    }



}
